package com.mane.controller;

import com.mane.entity.DTO.BaseDTO;
import com.mane.entity.PageData;
import com.mane.entity.Result;
import com.mane.entity.DTO.TrainerSearchDTO;
import com.mane.entity.VO.TrainerListVO;
import com.mane.pojo.Trainer;
import com.mane.service.ITrainerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/trainer")
@Slf4j
@Tag(name = "培训师管理")
public class TrainerController {

    @Autowired
    private ITrainerService trainerService;

    @Operation(summary = "查询所有")
    @GetMapping("/list")
    public Result<List<Trainer>> listSearch(){
        List<Trainer> list = trainerService.list();
        return Result.ok(list);
    }

    @Operation(summary = "分页查所有")
    @PostMapping("/list2")
    public Result<PageData<Trainer>> listSearch(@RequestBody BaseDTO baseDTO){
        PageData<Trainer> pageData = trainerService.list2(baseDTO);
        return Result.ok(pageData);
    }

    @Operation(summary = "分页+搜索")
    @PostMapping("/list/search")
    public Result<PageData<TrainerListVO>> listSearch(@RequestBody TrainerSearchDTO trainerSearchDTO){
        PageData<TrainerListVO> pageData = trainerService.listSearch(trainerSearchDTO);
        return Result.ok(pageData);
    }

    @Operation(summary = "新增")
    @PostMapping("/add")
    public Result<Trainer> add(@Valid @RequestBody Trainer trainer){
        trainerService.save(trainer);
        return Result.ok(trainer);
    }

    @Operation(summary = "批量删除")
    @DeleteMapping("/dels")
    public Result deleteBatch(@RequestParam List<Long> ids){
        boolean flag = trainerService.removeByIds(ids);
        return Result.commonByFlag(flag);
    }

    @Operation(summary = "根据ID查询")
    @GetMapping("/{id}")
    public Result<Trainer> getById(@PathVariable Long id){
        Trainer trainer = trainerService.getById(id);
        return Result.ok(trainer);
    }

    @Operation(summary = "根据ID编辑")
    @PutMapping("/update")
    public Result updateById(@Valid @RequestBody Trainer trainer){
        boolean flag = trainerService.updateById(trainer);
        return Result.commonByFlag(flag);
    }



}